package com.agritech;

import java.util.Scanner;

/**
 * 系统主类，提供登录和功能选择界面
 */
public class SystemApplication {
    private static final String ADMIN_USERNAME = "admin";
    private static final String ADMIN_PASSWORD = "admin123";
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        if (login()) {
            showMainMenu();
        }
    }

    private static boolean login() {
        System.out.println("=== 智能农机系统登录 ===");
        System.out.print("用户名：");
        String username = scanner.nextLine();
        System.out.print("密码：");
        String password = scanner.nextLine();

        if (ADMIN_USERNAME.equals(username) && ADMIN_PASSWORD.equals(password)) {
            System.out.println("用户 " + username + " 登录成功，角色：ADMIN");
            System.out.println(getCurrentTime() + " - 用户 " + username + " 登录成功");
            return true;
        } else {
            System.out.println("用户名或密码错误！");
            return false;
        }
    }

    private static void showMainMenu() {
        while (true) {
            System.out.println("\n=== 欢迎 管理员 (ADMIN) ===");
            System.out.println("1. 农机设备管理");
            System.out.println("2. 农田环境监测");
            System.out.println("3. 智能路径规划");
            System.out.println("4. 任务执行与控制");
            System.out.println("5. 系统管理");
            System.out.println("6. 退出系统");
            System.out.print("请选择操作：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符

            switch (choice) {
                case 1:
                    handleMachineManagement();
                    break;
                case 2:
                    handleEnvironmentMonitoring();
                    break;
                case 3:
                    handlePathPlanning();
                    break;
                case 4:
                    handleTaskExecution();
                    break;
                case 5:
                    handleSystemManagement();
                    break;
                case 6:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效的选择，请重试！");
            }
        }
    }

    private static void handleMachineManagement() {
        System.out.println("\n=== 农机设备管理 ===");
        System.out.println("1. 查看所有设备");
        System.out.println("2. 添加新设备");
        System.out.println("3. 设备状态监控");
        System.out.println("4. 返回主菜单");
        System.out.print("请选择操作：");

        int choice = scanner.nextInt();
        scanner.nextLine();

        switch (choice) {
            case 1:
                // 调用Main类中的相关功能
                Main.main(new String[]{});
                break;
            case 2:
                System.out.println("功能开发中...");
                break;
            case 3:
                System.out.println("功能开发中...");
                break;
            case 4:
                return;
            default:
                System.out.println("无效的选择！");
        }
    }

    private static void handleEnvironmentMonitoring() {
        System.out.println("\n=== 农田环境监测 ===");
        System.out.println("1. 实时环境数据");
        System.out.println("2. 历史数据查询");
        System.out.println("3. 环境预警设置");
        System.out.println("4. 返回主菜单");
        System.out.print("请选择操作：");

        int choice = scanner.nextInt();
        scanner.nextLine();

        if (choice == 1) {
            // 显示实时环境数据
            Main.main(new String[]{});
        } else {
            System.out.println("功能开发中...");
        }
    }

    private static void handlePathPlanning() {
        System.out.println("\n=== 智能路径规划 ===");
        System.out.println("1. 查看当前路径");
        System.out.println("2. 规划新路径");
        System.out.println("3. 路径优化");
        System.out.println("4. 返回主菜单");
        System.out.print("请选择操作：");

        int choice = scanner.nextInt();
        scanner.nextLine();

        if (choice == 1) {
            // 显示路径规划示例
            Main.main(new String[]{});
        } else {
            System.out.println("功能开发中...");
        }
    }

    private static void handleTaskExecution() {
        System.out.println("\n=== 任务执行与控制 ===");
        System.out.println("1. 查看当前任务");
        System.out.println("2. 创建新任务");
        System.out.println("3. 任务监控");
        System.out.println("4. 返回主菜单");
        System.out.print("请选择操作：");

        int choice = scanner.nextInt();
        scanner.nextLine();

        System.out.println("功能开发中...");
    }

    private static void handleSystemManagement() {
        System.out.println("\n=== 系统管理 ===");
        System.out.println("1. 用户管理");
        System.out.println("2. 权限设置");
        System.out.println("3. 系统日志");
        System.out.println("4. 返回主菜单");
        System.out.print("请选择操作：");

        int choice = scanner.nextInt();
        scanner.nextLine();

        System.out.println("功能开发中...");
    }

    private static String getCurrentTime() {
        return java.time.LocalDateTime.now().toString();
    }
}